//
//  LeftSortsViewController.m
//  LGDeckViewController
//
//  Created by jamie on 15/3/31.
//  Copyright (c) 2015年 Jamie-Ling. All rights reserved.
//

#import "LeftSortsViewController.h"
#import "AppDelegate.h"
#import "LoginViewController.h"
#import <MMDrawerController/MMDrawerController.h>
#import "TabbarController.h"
#import "NavigationController.h"
#import "UIViewController+CurrentController.h"
#import "AboutJeemaaViewController.h"
#define ExcursionHeight (iPhoneX ? 40.f : 0.f)


@interface LeftSortsViewController ()

@property (nonatomic, strong) UIImageView               *backView;
@property (nonatomic, strong) UIImageView               *headView;
@property (nonatomic, strong) UILabel                   *nameLabel;
@property (nonatomic, strong) UILabel                   *setMsgLabel;
@property (nonatomic, strong) UIButton                  *button_setMsg;
@property (nonatomic, strong) UIButton                  *button_cancel;
@property (nonatomic, strong) UIButton                  *button_about;
@property (nonatomic, strong) UILabel                   *pushMessage;

@end

@implementation LeftSortsViewController


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    //更新用户配置（昵称，消息设置，头像）
    [self updateUserProfile];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUI];
}

- (void)setUI{
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    _nameLabel =  [QuickUIConfig createLabelFrame:CGRectMake(24*Radio, 80*Radio+30-50/2+ExcursionHeight, 200, 50)
                                        backColor:[UIColor whiteColor]
                                             text:@"load..."
                                        textColor:[UIColor blackColor]
                                         textFont:[UIFont systemFontOfSize:25 weight:0.1]
                                    textAlignment:NSTextAlignmentLeft
                                    numberOfLines:1];
    [self.view addSubview:_nameLabel];
    
    _headView = [QuickUIConfig createImageViewFrame:CGRectMake(Screen_Width-70-24-55, 80*Radio+ExcursionHeight, 60, 60)
                                      imagePathType:UIImageViewPathTypeInter
                                          imageName:@""
                                       cornerRadius:30
                                      masksToBounds:YES];
    [self.view addSubview:_headView];

    _button_setMsg = [QuickUIConfig createBtnFrame:CGRectMake(24*Radio, 80*Radio+60+90+ExcursionHeight, 150, 40)
                                        buttonType:UIButtonTypeSystem
                                         backColor:[UIColor whiteColor]
                                         textColor:RGB(51, 51, 51, 1)
                                             title:@"消息设置"
                                          textFont:[UIFont systemFontOfSize:15]
                                     textAlignment:UIControlContentHorizontalAlignmentLeft
                                            target:self
                                            action:@selector(messageControl)];
    [_button_setMsg setEnlargeEdgeWithTop:0 right:Screen_Width-70-24*Radio bottom:0 left:0];
    [self.view addSubview:_button_setMsg];
    
    _button_about = [QuickUIConfig createBtnFrame:CGRectMake(24*Radio, 80*Radio+60+90+65+ExcursionHeight, Screen_Width-100-40, 40)
                                       buttonType:UIButtonTypeSystem
                                        backColor:[UIColor whiteColor]
                                        textColor:RGB(51, 51, 51, 1)
                                            title:@"关于译马网"
                                         textFont:[UIFont systemFontOfSize:15]
                                    textAlignment:UIControlContentHorizontalAlignmentLeft
                                           target:self
                                           action:@selector(aboutJeemaaWebsite)];
    [_button_about setEnlargeEdgeWithTop:0 right:Screen_Width-70-24*Radio bottom:0 left:0];
    [self.view addSubview:_button_about];
    
    _button_cancel = [QuickUIConfig createBtnFrame:CGRectMake(24*Radio, 80*Radio+60+90+130+ExcursionHeight, Screen_Width-100-40, 40)
                                        buttonType:UIButtonTypeSystem
                                         backColor:[UIColor whiteColor]
                                         textColor:RGB(51, 51, 51, 1)
                                             title:@"退出账号"
                                          textFont:[UIFont systemFontOfSize:15]
                                     textAlignment:UIControlContentHorizontalAlignmentLeft
                                            target:self
                                            action:@selector(logoutCurrentAccount)];
    [_button_cancel setEnlargeEdgeWithTop:0 right:Screen_Width-70-24*Radio bottom:0 left:0];
    [self.view addSubview:_button_cancel];
    
    UIImageView *abtview = [QuickUIConfig createImageViewFrame:CGRectMake(0,0, 20*Radio, 20*Radio)
                                                 imagePathType:UIImageViewPathTypeLocal
                                                     imageName:@"个人中心_更多"
                                                  cornerRadius:0
                                                 masksToBounds:NO];
    abtview.center = CGPointMake(Screen_Width-100, _button_about.center.y);
    [self.view addSubview:abtview];
    
    UIImageView *setview = [QuickUIConfig createImageViewFrame:CGRectMake(0,0, 20*Radio, 20*Radio)
                                                 imagePathType:UIImageViewPathTypeLocal
                                                     imageName:@"个人中心_更多"
                                                  cornerRadius:0
                                                 masksToBounds:NO];
    setview.center = CGPointMake(Screen_Width-100, _button_setMsg.center.y);
    [self.view addSubview:setview];
    
    UIView *lineTop = [QuickUIConfig createViewFrame:CGRectMake(20*Radio,_button_setMsg.frame.origin.y+_button_setMsg.frame.size.height+10,Screen_Width-70-20*Radio*2 , 1)
                                           backColor:RGB(246, 246, 246, 1)
                                        cornerRadius:0
                                       masksToBounds:NO];
    [self.view addSubview:lineTop];
    
    UIView *lineMid = [QuickUIConfig createViewFrame:CGRectMake(20*Radio,_button_about.frame.origin.y+_button_about.frame.size.height+10,Screen_Width-70-20*Radio*2 , 1)
                                           backColor:RGB(246, 246, 246, 1)
                                        cornerRadius:0
                                       masksToBounds:NO];
    [self.view addSubview:lineMid];
    
    UIView *lineFot = [QuickUIConfig createViewFrame:CGRectMake(20*Radio,_button_cancel.frame.origin.y+_button_cancel.frame.size.height+10,Screen_Width-70-20*Radio*2 , 1)
                                           backColor:RGB(246, 246, 246, 1)
                                        cornerRadius:0
                                       masksToBounds:NO];
    [self.view addSubview:lineFot];
    
    _pushMessage = [QuickUIConfig createLabelFrame:CGRectMake(Screen_Width-150, setview.frame.origin.y, 50, 20)
                                         backColor:[UIColor clearColor]
                                              text:shareData.userIfOpenPush ? @"已开启" : @"已关闭"
                                         textColor:RGB(182, 182, 182, 1)
                                          textFont:[UIFont systemFontOfSize:FLEXIBLE(13)]
                                     textAlignment:NSTextAlignmentLeft
                                     numberOfLines:1];
    [self.view addSubview:_pushMessage];

}

#pragma mark -- 退出登录
- (void)logoutCurrentAccount{
    
    [SMAlert setAlertBackgroundColor:[UIColor colorWithWhite:0 alpha:0.6]];
    [SMAlert setContentTextColor:[UIColor blackColor]];
    [SMAlert setConfirmBtTitleColor:RGB(35, 138, 247, 1)];
    [SMAlert setCancleBtTitleColor:RGB(35, 138, 247, 1)];
    [SMAlert setContentFont:[UIFont systemFontOfSize:FLEXIBLE(17)]];
    [SMAlert showContent:@"退出当前账号?" confirmButton:[SMButton initWithTitle:@"确定" clickAction:^{
        [SMAlert hide];
        [JeemaaNotificationCenter postNotificationName:@"loginoutAtLoading" object:nil];
        [self loginoutCurrentaccount];
    }]cancleButton:[SMButton initWithTitle:@"取消" clickAction:nil]];
}

- (void)loginoutCurrentaccount{
    
    NSDictionary *dic = @{@"userName":[UserDefaults objectForKey:@"account"]};
    [AvailableNetWorking postWithUrl:loginout_connector params:dic successHandler:^(id responseObject) {
        if ([responseObject[@"code"] intValue] == 200) {
            
            //设定用户离线
            [[MJ_UserMessage sharedInstance] userLogOut];
            
            MMDrawerController *currentVC = (MMDrawerController *)[UIViewController currentViewController];
            [currentVC closeDrawerAnimated:YES completion:nil];
            LoginViewController *loginVC = [[LoginViewController alloc] init];
            [UIApplication sharedApplication].keyWindow.rootViewController = loginVC;
        }
        else{
            [TopAlertView SetUpbackgroundColor:AlertRedColor andStayTime:2.0 andImageName:@"" andTitleStr:responseObject[@"message"] andTitleColor:[UIColor whiteColor] andCancel:YES];
            [JeemaaNotificationCenter postNotificationName:@"hiddenHudWhenLoginoutFaild" object:nil];
        }
    } falureHandler:^(NSError *error) {
        [JeemaaNotificationCenter postNotificationName:@"hiddenHudWhenLoginoutFaild" object:nil];
    }];
}

- (void)cleanAllCache{
    
    //设定用户离线
    [[MJ_UserMessage sharedInstance] userLogOut];
    /***/
    orderSingleton.historyCount = 0;
    shareData.loginByLoginViewcontroller = NO;
    shareData.atProjectPart = NO;
}

- (void)aboutJeemaaWebsite{

    AboutJeemaaViewController *aboutVC = [[AboutJeemaaViewController alloc] init];
    MMDrawerController *currentVC = (MMDrawerController *)[UIViewController currentViewController];
    //跳转
    if ([currentVC.centerViewController isKindOfClass:[UITabBarController class]]) {
        
        TabbarController *tab = (TabbarController *)currentVC.centerViewController;
        NavigationController *nav = (NavigationController *)tab.selectedViewController;
        [nav pushViewController:aboutVC animated:YES];
        
    }else{
        NavigationController *nav = (NavigationController *)currentVC.centerViewController;
        [nav pushViewController:aboutVC animated:YES];
    }
    //关闭抽屉
    [currentVC closeDrawerAnimated:YES completion:^(BOOL finished) {
//        //设置打开抽屉模式为MMOpenDrawerGestureModeNone，也就是没有任何效果。
//        [currentVC setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeNone];
    }];

}

- (void)messageControl{

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
    
}

- (void)updateUserProfile{

    _nameLabel.text = shareData.userNick;
    
    [_headView sd_setImageWithURL:[NSURL URLWithString:shareData.headURL] placeholderImage:[UIImage imageNamed:@"user"]];
    //判断是否开启推送
    UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSettings];
    
    if (setting.types == UIUserNotificationTypeNone) {
        _pushMessage.text = @"已关闭";
    }else {
        _pushMessage.text = @"已开启";
    }
    
}


@end
